﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Storage.Mapping.Bucket;
using Storage.Core.Caching;
using Storage.Core.Data;
using Storage.Entities;
using Storage.Mapping.AppStorage;

namespace Storage.Services.Bucket
{
    public class BucketService : IBucketService
    {
        private const string MODEL_KEY = "Storage.services.bucket";
        private ICacheManager _cacheManager;
        private IRepository<Entities.Bucket> _bucketRepository;

        public BucketService(ICacheManager cacheManager,
            IRepository<Entities.Bucket> bucketRepository)
        {
            this._cacheManager = cacheManager;
            this._bucketRepository = bucketRepository;
        }

        /// <summary>
        /// 获取所有并缓存
        /// </summary>
        /// <returns></returns>
        public List<BucketMapping> getAllCache()
        {
            return _cacheManager.get<List<BucketMapping>>(MODEL_KEY, 24 * 60, () =>
                {
                    return _bucketRepository.Table.Select(item => new BucketMapping()
                    {
                        Id = item.Id,
                        AppStorageId = item.AppStorageId,
                        Name = item.Name,
                        CreationTime = item.CreationTime,
                        AppStorage = new AppStorageMapping()
                        {
                            AppKey = item.AppStorage.AppKey,
                            SecretKey = item.AppStorage.SecretKey,
                            RootDirectory =item.AppStorage.RootDirectory
                        }
                    }).ToList();
                });
        }

        /// <summary>
        /// 通过名称获取
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public BucketMapping getByName(string name)
        {
            var list = getAllCache();
            if (list == null)
                return null;
            return list.FirstOrDefault(o => o.Name == name);
        }

        /// <summary>
        /// 通过id获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BucketMapping getById(Guid id)
        {
            var list = getAllCache();
            if (list == null)
                return null;
            return list.FirstOrDefault(o => o.Id == id);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool existName(Guid id, string name)
        {
            return _bucketRepository.Table.Any(o => o.Name == name && o.Id != id);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        public void insertBucket(Entities.Bucket entity)
        {
            if (existName(entity.Id, entity.Name))
                return;
            _bucketRepository.insert(entity);
            _cacheManager.remove(MODEL_KEY);
        }

    }
}
